﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShopOnline.Models.DAL
{
    public class DAL_Product
    {
        private ShopOnline.Models.ShopOnlineDBDataContext SC_DB;
        public DAL_Product()
        {
            SC_DB = new ShopOnlineDBDataContext();
        }


        public List<Product> getListProduct()
        {
           return SC_DB.Products.ToList();
        }
        public List<Product> getListProductShow()
        {
            var list = (from c in SC_DB.Products where c.show == true orderby c.piority select c).ToList();
            return list;
        }

        public Product getProductById(int ProductId)
        {
            return (from c in SC_DB.Products where c.productId==ProductId select c).FirstOrDefault();
        }

        public int CreateProduct(Product Product)
        {
            int result = 0;
            try
            {
                SC_DB.Products.InsertOnSubmit(Product);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {
                
            }
            return result;
        }

        public int EditCProduct(Product pro)
        {
            int result = 0;
            try
            {
                var Product = (from c in SC_DB.Products where c.productId == pro.productId select c).FirstOrDefault();
                Product.description = pro.description;
                Product.image = pro.image;
                Product.information = pro.information;
                Product.piority = pro.piority;
                Product.productName = pro.productName;
                Product.show = pro.show;
                Product.QuatityPerUnits = pro.QuatityPerUnits;
                Product.showAdvert = pro.showAdvert;
                Product.unitInStock = pro.unitInStock;
                Product.unitPrice = pro.unitPrice;
                Product.unitsOnOrder = pro.unitsOnOrder;
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }
        public int DeleteProduct(Product Pro)
        {
            int result = 0;
            try
            {
                var Product = (from c in SC_DB.Products where c.productId == Pro.productId select c).FirstOrDefault();
                SC_DB.Products.DeleteOnSubmit(Product);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

        public List<Product> getTop2Product()
        {
            return (from pre in SC_DB.Products orderby pre.piority, pre.productId descending where pre.show == true select pre).Skip(0).Take(2).ToList();
        }
        public List<Product> getList6Orther(int id)
        {
            return (from c in SC_DB.Products orderby c.piority, c.productId descending where c.productId != id select c).Take(6).ToList();
        }
        public List<Product> get6Product(int n,int skip, int catelogyId)
        {

            return (from pre in SC_DB.Products orderby pre.productId descending where (pre.show == true && pre.productCatelogyId == catelogyId) select pre).Skip(n).Take(skip).ToList();
        }


        public int getCatelogyIdDefaut()
        {
            int catelogyIdDefault = (from p in SC_DB.Catelogies orderby p.catelogyId select p.catelogyId).First();
            if (catelogyIdDefault==null)
            {
                catelogyIdDefault = 0;
            }
            return catelogyIdDefault;
        }
        public List<Product> getListProductByCateId(int cateId)
        {
            return (from p in SC_DB.Products where (p.show == true && p.productCatelogyId == cateId) orderby p.piority select p).ToList();
        }
    }
}